package com.xingkeduo.business.operation.json;

import java.util.Map;

import org.springframework.stereotype.Component;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.xingkeduo.util.pojo.base.BaseJson;
import com.xingkeduo.util.pojo.enums.SexEnum;
import com.xingkeduo.util.pojo.enums.YesNoEnum;
import com.xingkeduo.util.pojo.exception.InputParamException;

/**
 * @Description:
 * @Author:gaozifeng
 * @Date 2019/3/1 15:02
 **/
@Component
public class ShopsJTO extends BaseJson {

	
	public static ObjectNode toJsonForSendSms(Map<String, Object> map) throws Throwable {
        ObjectNode root = MAPPER.createObjectNode();
        
        if(map!=null) {
        	if("00".equals(map.get("error"))) {
        		root.put("is_new", 0);
        	}else if("01".equals(map.get("error"))){
        		root.put("is_new", 1);
        	}
        }
        return root;
    }
	
	 
	 


    public static SexEnum validSex(JsonNode node, YesNoEnum isRequired) throws Throwable {
        String key = "sex";
        String value = node.path(key).asText().trim();
        if (value.equals("FEMALE") || value.equals("1")) {
            value = "1";
        } else if (value.equals("MALE") || value.equals("0")) {
            value = "0";
        } else {
            return null;
        }
        return SexEnum.getSex(value);
    }

    public static int validCity(JsonNode node, YesNoEnum isRequired) throws Throwable {
        String key = "city";
        if (YesNoEnum.YES == isRequired && !node.has(key))
            throw new InputParamException(InputParamException.Type.NULL, key);

        if (node.has(key)) {
            String value = node.path(key).asText().trim();
            if (value.length() > 3)
                throw new InputParamException(InputParamException.Type.INVALID, key + " error");
            return Integer.valueOf(value);
        }
        return 0;
    }
}
